//
//  ChatTimeHelper.swift
//  Exchange
//
//  Created by rui on 2018/5/9.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class ChatTimeHelper: NSObject {
    
    class func needAddMinuteModel(preModel: ChatModel, curModel: ChatModel) -> Bool {
        guard let preTime = preModel.time  else {
            return false
        }
        guard let curTime = curModel.time else {
            return false
        }
        let preDate = Date(timeIntervalSince1970: preTime)
        let curDate = Date(timeIntervalSince1970: curTime)
        let comps: Set<Calendar.Component> = [.year,.month,.day,.hour,.minute]
        
        let calendar = NSCalendar.current
        
        let preDataComp = calendar.dateComponents(comps, from: preDate)
        let curDataComp = calendar.dateComponents(comps, from: curDate)
        
        let year =  curDataComp.year! - preDataComp.year!
        let month = curDataComp.month! - preDataComp.month!
        let day = curDataComp.day! - preDataComp.day!
        let hour = curDataComp.hour! - preDataComp.hour!
        let minute = curDataComp.minute! - preDataComp.minute!
        
        if year > 0 || month > 0 || day > 0 || hour > 0 {
            return true
        } else if minute >= 5 {
            return true
        } else {
            return false
        }
    }
    
    
    class func chatTimeString(with modelTime: TimeInterval?) -> String? {
        guard let time = modelTime else {
            return nil
        }
        // 消息时间
        let date = Date(timeIntervalSince1970: time)
    
        let curDate = Date()
        
        let comps: Set<Calendar.Component> = [.year,.month,.day,.hour,.minute,.second,.weekday]
        
        let calendar = NSCalendar.current
        let dataComp = calendar.dateComponents(comps, from: date)
        let curDataComp = calendar.dateComponents(comps, from: curDate)
        
        let year =  curDataComp.year! - dataComp.year!
        let month = curDataComp.month! - dataComp.month!
        let day = curDataComp.day! - dataComp.day!
        
        if year != 0 {
            return String(format: "%d/%d/%d %d:%02d", dataComp.year!, dataComp.month!, dataComp.day!, dataComp.hour!, dataComp.minute!)
        } else if year == 0 {
            if month > 0 || day > 7 {
                return String(format: "%d/%d/ %d:%02d", dataComp.month!, dataComp.day!, dataComp.hour!, dataComp.minute!)
            } else if day >= 2 {
                return String(format: "%@ %d:%02d", weekdayName(date: date), dataComp.hour!, dataComp.minute!)
            }else if day == 1 {
                return String(format: "%@ %d:%02d", kText("昨天"),dataComp.hour!, dataComp.minute!)
            } else {
                return String(format: "%d:%02d",dataComp.hour!, dataComp.minute!)
            }
        }
        return ""
    }
    
    class func weekdayName(date: Date) -> String {
        let dataFormatter = DateFormatter()
        dataFormatter.dateFormat = "EEEE"
        let str = dataFormatter.string(from: date)
        return str
    }
    
    
}
